package dsVbck.backing;

import dsVbck.dsVbck;

import dsVbck.resource.dsVbckBundle;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import java.util.Locale;


import java.util.ResourceBundle;

import java.util.TimeZone;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;

import javax.el.ValueExpression;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;


import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.adf.view.rich.component.rich.layout.RichPanelBorderLayout;
import oracle.adf.view.rich.component.rich.nav.RichCommandLink;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.stellent.ridc.IdcClient;
import oracle.stellent.ridc.IdcContext;
import oracle.stellent.ridc.model.DataBinder;
import oracle.stellent.ridc.model.DataObject;
import oracle.stellent.ridc.model.DataResultSet;

import oracle.stellent.ridc.protocol.ServiceResponse;

import oracle.webcenter.framework.service.JSFUtils;

import org.apache.myfaces.trinidad.model.RowKeySet;

import org.apache.myfaces.trinidadinternal.taglib.util.MethodExpressionMethodBinding;

import portlet.Global;

public class dsVbckBacking {
    private String mPortletTitle = "";
    private String sohieuvanban;
    private String trichyeu;
    private Date tungay;
    private Date denngay;
    private String noidung;
    private List<DataObject> lstLinhvuc;
    private List<DataObject> lstLoaivanban;
    private List<DataObject> lstCoquanbanhanh;
    private List<DataObject> lstVBCK = new ArrayList<DataObject>();

    private List<SelectItem> lstLinhvucItems = new ArrayList<SelectItem>();
    private List<SelectItem> lstLoaivanbanItems = new ArrayList<SelectItem>();
    private List<SelectItem> lstCoquanbanhanhItems = new ArrayList<SelectItem>();

    private String linhvucValue;
    private String loaivanbanValue;
    private String coquanbanhanhValue;
    private String functionRequest = "";
    private RowKeySet rowkeyselect;
    private RichTable vbckTable;
    private int rowonpage;
    private String chitieturl = "";
    private Date ngaybanhanh;
    private String tieude = "";
    private int currentPage = 1;
    private int selectedPage = 1;
    private int numberOfResult = 10;
    private static List<String> pagesnumber = new ArrayList<String>();
    private boolean hasnextpage;
    private boolean firstpage;
    private RichInputText inputcurrentpage;
    private RichInputText inputhasnext;
    private String tempcurrenpage;
    private String tempselectpage;
    private String ngonnguid = "vi";
    private RichPanelBorderLayout pagingPanelGroup =  new RichPanelBorderLayout();
    private String initpram;
    private TimeZone timezone = TimeZone.getTimeZone("GMT+7");
    private Locale portalLocale = new Locale("vi", "VN");
    private RichInputText txtSohieuvanban;
    private RichInputText txtTrichyeu;
    private RichInputText txtNoidung;
    private String  totalRows;
    private int numberOfPage;


    public dsVbckBacking() {
        if (!AdfFacesContext.getCurrentInstance().isPostback()) {
            firstpage = true;
            currentPage = 1;
            selectedPage = 1;
            numberOfResult = 10;
            hasnextpage = false;
//            Global.setPageFlowScope("pagesnumber", pagesnumber);
//            Global.setPageFlowScope("currentPage", selectedPage);
//            Global.setPageFlowScope("hasnextpage", hasnextpage);
            getListWithPaging(1);          
        }

    }

    public String getPortletTitle() {
        if (mPortletTitle == null) {
            try {
                PortletRequest request =
                    (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
                mPortletTitle = request.getParameter("tieude");
            } catch (Exception e) {
                // TODO: Add catch code
                e.printStackTrace();
            }
        }
        return mPortletTitle;
    }

    public String getPortletTitleLabel() {
        String resource = getResource(dsVbckBundle.PORTLETTITLE);
        return resource;
    }

    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }

    public String applyAction() throws Exception {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(dsVbck.PORTLETTITLE_KEY,
                              mPortletTitle.split(","));
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(dsVbckBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response =
            (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(dsVbckBundle.OK_LABEL);
        return resource;
    }

    private String getResource(String resourceKey) {
        Locale locale = loadPortalLocale();
        ResourceBundle bundle =
            ResourceBundle.getBundle("portlet.PortletsBundle", locale);
        return bundle.getString(resourceKey);
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return Kiểu ngôn ngữ
     */
    private Locale loadPortalLocale() {
        
        String language = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("ngonnguid") != null) {
                language = request.getParameter("ngonnguid");
                portalLocale = new Locale(language);
            }

        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return portalLocale;
    }

    public String getEMPTY_DATA() {
        String resource = getResource("EMPTY_DATA");
        return resource;
    }
    public String getNGAY_BAN_HANH_TU_NGAY() {
        String resource = getResource("NGAY_BAN_HANH_TU_NGAY");
        return resource;
    }

    public String getSO_HIEU_VAN_BAN() {
        String resource = getResource("SO_HIEU_VAN_BAN");
        return resource;
    }

    public String getDEN_NGAY() {
        String resource = getResource("DEN_NGAY");
        return resource;
    }

    public String getTRICH_YEU() {
        String resource = getResource("TRICH_YEU");
        return resource;
    }

    public String getLINH_VUC() {
        String resource = getResource("LINH_VUC");
        return resource;
    }

    public String getLOAI_VAN_BAN() {
        String resource = getResource("LOAI_VAN_BAN");
        return resource;
    }

    public String getTAT_CA() {
        String resource = getResource("TAT_CA");
        return resource;
    }

    public String getCO_QUAN_BAN_HANH() {
        String resource = getResource("CO_QUAN_BAN_HANH");
        return resource;
    }

    public String getNOI_DUNG() {
        String resource = getResource("NOI_DUNG");
        return resource;
    }

    public String getTIM_KIEM() {
        String resource = getResource("TIM_KIEM");
        return resource;
    }

    public String getNGAY_BAN_HANH() {
        String resource = getResource("NGAY_BAN_HANH");
        return resource;
    }

    public String getTU_NGAY() {
        String resource = getResource("TU_NGAY");
        return resource;
    }

    public String getTRANG_SAU() {
        String resource = getResource("TRANG_SAU");
        return resource;
    }

    public String getTRANG_TRUOC() {
        String resource = getResource("TRANG_TRUOC");
        return resource;
    }

    public String getTIM_KIEM_VAN_BAN() {
        String resource = getResource("TIM_KIEM_VAN_BAN");
        return resource;
    }

    public String getDANH_SACH_VAN_BAN() {
        String resource = getResource("DANH_SACH_VAN_BAN");
        return resource;
    }

    /**
     * @creator
     * @comment doc du lieu cua 1 bang trong csdl UCM
     * @param viewName ten view trong UCM
     * @param tableName ten bang du lieu trong UCM
     * @param client
     * @param userContext
     * @return
     */
    private DataResultSet getViewInfor(String viewName, String tableName,
                                       IdcClient client,
                                       IdcContext userContext) {
        DataResultSet obj = null;
        try {
            DataBinder dataBinder = client.createBinder();
            dataBinder.putLocal("IdcService", "GET_SCHEMA_VIEW_VALUES");
            dataBinder.putLocal("schViewName", viewName.toUpperCase());
            ServiceResponse ridc_response;
            ridc_response = client.sendRequest(userContext, dataBinder);
            DataBinder binder = ridc_response.getResponseAsBinder();
            obj = binder.getResultSet(tableName.toUpperCase());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return obj;
    }

    /**
     * @creator
     * @comment
     */
    private void getdslinhvuc() {
        DataResultSet rs =
            getViewInfor("VBCK_LINHVUC_VIEW", "VBCK_LINHVUC", Global.getIdcClient(),
                         Global.getUserContext());
        lstLinhvuc = rs.getRows();
        for (DataObject obj : lstLinhvuc) {
            if (obj.get("NGONNGU_ID").toString().equals(getNgonnguid())) {
                SelectItem item = new SelectItem();
                item.setLabel(obj.get("TEN"));
                item.setValue(obj.get("ID"));
                lstLinhvucItems.add(item);
            }
        }

    }

    /**
     * @creator
     * @comment
     */
    private void getdsloaibanban() {
        DataResultSet rs =
            getViewInfor("VBCK_LOAI_VIEW", "VBCK_LOAI", Global.getIdcClient(),
                         Global.getUserContext());
        lstLoaivanban = rs.getRows();
        for (DataObject obj : lstLoaivanban) {
            if (obj.get("NGONNGU_ID").toString().equals(getNgonnguid())) {
                SelectItem item = new SelectItem();
                item.setLabel(obj.get("TEN"));
                item.setValue(obj.get("ID"));
                lstLoaivanbanItems.add(item);
            }
        }

    }

    /**
     * @creator
     * @comment
     */
    private void getdscoquanbanhanh() {
        DataResultSet rs =
            getViewInfor("VBCK_CQBH_VIEW", "VBCK_CQBH", Global.getIdcClient(),
                         Global.getUserContext());
        lstCoquanbanhanh = rs.getRows();
        for (DataObject obj : lstCoquanbanhanh) {
            if (obj.get("NGONNGU_ID").toString().equals(getNgonnguid())) {
                SelectItem item = new SelectItem();
                item.setLabel(obj.get("TEN"));
                item.setValue(obj.get("ID"));
                lstCoquanbanhanhItems.add(item);
            }
        }

    }

    /**
     * @creator
     * @comment Thu tuc tim kiem noi dung trong UCM
     * @param queryString
     * @param sortField
     * @param numberofResult 0: is not set
     * @param startRow 0: is not set
     * @param endRow 0: is not set
     * @param sortOrder ASC, DESC
     * @param client
     * @param userContext
     * @return
     */
    private DataResultSet SearchUCMContent(String queryString,
                                           String sortField,
                                           int numberofResult, int startRow,
                                           int endRow, String sortOrder,
                                           IdcClient client,
                                           IdcContext userContext) {
        DataResultSet obj = null;
        try {
            DataBinder dataBinder = client.createBinder();
            dataBinder.putLocal("IdcService", "GET_SEARCH_RESULTS");
            dataBinder.putLocal("QueryText", queryString);
            dataBinder.putLocal("SortField", sortField);
            if (numberofResult > 0)
                dataBinder.putLocal("ResultCount",
                                    String.valueOf(numberofResult));
            if (startRow > 0)
                dataBinder.putLocal("StartRow", String.valueOf(startRow));
            if (endRow > 0)
                dataBinder.putLocal("EndRow", String.valueOf(endRow));
            if (sortOrder != null && sortOrder.length() > 0)
                dataBinder.putLocal("SortOrder", sortOrder);
            ServiceResponse ridc_response;
            ridc_response = client.sendRequest(userContext, dataBinder);
            DataBinder binder = ridc_response.getResponseAsBinder();
            totalRows = binder.getLocal("TotalRows");
            obj = binder.getResultSet("SearchResults");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return obj;
    }

    /**
     * @creator
     * @comment
     * @param tungay
     * @param denngay
     * @param sohieuvanban
     * @param trichyeu
     * @param linhvuc
     * @param loaivanban
     * @param coquanbanhanh
     * @param ngonnguid
     * @return
     */
    private String createQueryString(Date tungay, Date denngay,
                                     String sohieuvanban, String trichyeu,
                                     String linhvuc, String loaivanban,
                                     String coquanbanhanh, String ngonnguid) {
        String queryString = "dDocType <contains> `VBCK`";
        if (tungay != null && denngay != null) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            queryString +=
                    " <AND> xmNgaybanhanh >= `" + df.format(tungay) + "`";
            queryString +=
                    " <AND> xmNgaybanhanh <= `" + df.format(denngay) + "`";
        }
        else if((tungay != null && denngay == null)){
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            queryString +=
                    " <AND> xmNgaybanhanh >= `" + df.format(tungay) + "`";
        }
        else if(tungay == null && denngay != null)
        {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            queryString +=
                    " <AND> xmNgaybanhanh <= `" + df.format(denngay) + "`";
        }
        if (sohieuvanban != null && !sohieuvanban.equals("")) {       
            sohieuvanban = sohieuvanban.trim();
            txtSohieuvanban.setValue(sohieuvanban);
            sohieuvanban = sohieuvanban.toLowerCase().replaceAll("<qsch>", "");
            sohieuvanban =
                    sohieuvanban.toLowerCase().replaceAll("</qsch>", "");
            queryString +=
                    " <AND> <qsch>dDocTitle:" + sohieuvanban + "</qsch>";
            
        }
        if (trichyeu != null && !trichyeu.equals("")) {
            trichyeu = trichyeu.trim();
            txtTrichyeu.setValue(trichyeu);
            trichyeu = trichyeu.toLowerCase().replaceAll("<qsch>", "");
            trichyeu = trichyeu.toLowerCase().replaceAll("</qsch>", "");
            queryString += " <AND> <qsch>xComments:" + trichyeu + "</qsch>";
           
        }
        if (linhvuc != null && !linhvuc.equals(""))
            queryString += " <AND> xmLinhvuc <matches> `" + linhvuc + "`";
        if (loaivanban != null && !loaivanban.equals(""))
        {
            String arrLoai[] = null;
            if(loaivanbanValue.contains("-")) {
                try {
                    loaivanban = loaivanban.replace("-", "");
                    if(loaivanban.contains(","))
                        arrLoai = loaivanban.split(",");
                    if(arrLoai!=null) {
                        for(int i=0;i<arrLoai.length;i++) {
                            queryString += " <AND> <NOT> xmLoaivb <matches> `" + arrLoai[i] + "`";
                        }
                    }
                } catch (Exception e) {
                    // TODO: Add catch code
                    e.printStackTrace();
                }
            }
            else
                queryString += " <AND> xmLoaivb <matches> `" + loaivanban + "`";
        }
        if (coquanbanhanh != null && !coquanbanhanh.equals(""))
            queryString += " <AND> xmCqbh <matches> `" + coquanbanhanh + "`";
        if (ngonnguid != null && !ngonnguid.equals(""))
            queryString += " <AND> xmNgonngu <matches> `" + ngonnguid + "`";
        if (noidung != null && !noidung.equals("")) {
            noidung = noidung.trim();
            txtNoidung.setValue(noidung);
            noidung = noidung.toLowerCase().replaceAll("<ftx>", "");
            noidung = noidung.toLowerCase().replaceAll("</ftx>", "");
            queryString += " <AND> (<ftx>" + noidung + "</ftx>)";
          
        }
        return queryString;
    }

    /**
     * @creator
     * @comment
     * @return
     */
    public int getNumberOfResult() {
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            numberOfResult =
                    Integer.parseInt(request.getParameter("sobanghi"));

        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return numberOfResult;
    }

    /**
     * @creator
     * @comment
     * @param currentpage
     */
    public void generatePrevLink(int currentpage) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        MethodExpression methodExpression =
            elFactory.createMethodExpression(elContext, "#{dsvbckbacking.toprevPage}",
                                             null,
                                             new Class[] { ActionEvent.class });       
        MethodExpressionMethodBinding methodExpressionMethodBinding =
            new MethodExpressionMethodBinding(methodExpression);
        RichCommandLink commandLink = new RichCommandLink();
        commandLink.setPartialSubmit(true);
        commandLink.setText(getTRANG_TRUOC());
        commandLink.getAttributes().put("actionListener",
                                        methodExpressionMethodBinding);
        commandLink.getAttributes().put("prevpage", currentpage - 1);
        commandLink.setInlineStyle("margin-right:5px;");        
        if(currentpage <2)
            commandLink.setDisabled(true);
        else
            commandLink.setDisabled(false);
        pagingPanelGroup.getChildren().add(commandLink);
    }

    /**
     * @creator
     * @comment
     * @param currentpage
     */
    public void generateNextLink(int currentpage) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        MethodExpression methodExpression =
            elFactory.createMethodExpression(elContext, "#{dsvbckbacking.nextPage}",
                                             null,
                                             new Class[] { ActionEvent.class });       
        MethodExpressionMethodBinding methodExpressionMethodBinding =
            new MethodExpressionMethodBinding(methodExpression);
        RichCommandLink commandLink = new RichCommandLink();
        commandLink.setPartialSubmit(true);
        commandLink.setText(getTRANG_SAU());
        commandLink.getAttributes().put("actionListener",
                                        methodExpressionMethodBinding);
        commandLink.getAttributes().put("nexpage", currentpage + 1);
        commandLink.setInlineStyle("margin-left:5px;");
        if(! hasnextpage)
            commandLink.setDisabled(true);      
        pagingPanelGroup.getChildren().add(commandLink);
    }

    /**
     * @creator
     * @comment
     * @param pagenumber
     */
    public void generatePagenumber(int pagenumber) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        MethodExpression methodExpression =
            elFactory.createMethodExpression(elContext, "#{dsvbckbacking.pageSeleted}",
                                             null,
                                             new Class[] { ActionEvent.class });
        
        MethodExpressionMethodBinding methodExpressionMethodBinding =
            new MethodExpressionMethodBinding(methodExpression);
        RichCommandLink commandLink = new RichCommandLink();
        commandLink.setPartialSubmit(true);
        commandLink.setText(String.valueOf(pagenumber));
        if(pagenumber == selectedPage)
            commandLink.setInlineStyle("font-weight:bold; margin:0 2px;");
        else
            commandLink.setInlineStyle("margin:0 2px;");            
        commandLink.getAttributes().put("actionListener",
                                        methodExpressionMethodBinding);
        commandLink.getAttributes().put("selectedpage", pagenumber);
        pagingPanelGroup.getChildren().add(commandLink);
    }

    /**
     * @creator
     * @comment
     * @param value
     */
    public void generateDots(String value) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        RichOutputText ouputtext = new RichOutputText();
        ouputtext.setValue(value);
        pagingPanelGroup.getChildren().add(ouputtext);
    }

    /**
     * @creator
     * @comment
     * @param selectedPage
     */
    private void getListWithPaging(int selectedPage) {
        int startRow = 1;

        try {
            startRow = getNumberOfResult() * (selectedPage - 1) + 1;
            if (loaivanbanValue == null || loaivanbanValue.isEmpty())
                try {
                    PortletRequest request =
                        (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
                    loaivanbanValue =
                            request.getParameter("loaivb").toString();

                } catch (Exception e) {
                    // TODO: Add catch code
                    e.printStackTrace();
                }
           
            String querystring =
                createQueryString(tungay, denngay, sohieuvanban, trichyeu,
                                  linhvucValue, loaivanbanValue,
                                  coquanbanhanhValue, getNgonnguid());
            //System.out.print(querystring);
            DataResultSet drs =
                SearchUCMContent(querystring, "xmNgaybanhanh", numberOfResult,
                                 startRow, 0, "DESC", Global.getIdcClient(),
                                 Global.getUserContext());
            lstVBCK = drs.getRows();
//            if (pagesnumber.size() > 0)
//                pagesnumber.clear();
                        int rowscount = 0;
            if (!totalRows.equals("0")) {
                rowscount = Integer.parseInt(totalRows);
                numberOfPage = rowscount % numberOfResult > 0 ? (rowscount / numberOfResult + 1) : rowscount / numberOfResult;
            }
            if( pagingPanelGroup.getChildCount()>0)
                pagingPanelGroup.getChildren().clear();
            
            generatePrevLink(selectedPage);
            if (numberOfPage >= 5) {
                if(selectedPage<numberOfPage)
                    hasnextpage = true;
                if (selectedPage - 2 >= 0 && selectedPage + 2 <= numberOfPage) {
                    if (selectedPage - 2 > 1)
                    {   
                        generatePagenumber(1);
                        generateDots("...");
                        
                    }
                    if(selectedPage>2)
                        generatePagenumber(selectedPage - 2);
                    generatePagenumber(selectedPage - 1);
                    generatePagenumber(selectedPage);
                    generatePagenumber(selectedPage + 1);
                    generatePagenumber(selectedPage + 2);
                    if(selectedPage +2<5)
                        generatePagenumber(selectedPage + 3);
                    if (selectedPage + 2 < numberOfPage)
                    {
                        generateDots("...");
                        generatePagenumber(numberOfPage);
                       
                    }
                   
                } else if (selectedPage <2) {
                    for(int i=selectedPage;i<=selectedPage + 4;i++ )  
                        generatePagenumber(i);
                    generateDots("...");
                    generatePagenumber(numberOfPage);
                   
                } else if(selectedPage + 2 > numberOfPage){
                    generatePagenumber(1);
                    generateDots("...");
                    if(selectedPage==numberOfPage)
                        for(int i=selectedPage -4;i<=selectedPage;i ++)
                            generatePagenumber(i);
                    else
                        for(int i=selectedPage -3;i<=selectedPage +1;i ++)
                            generatePagenumber(i);                        
                }                    
            }
            else
            {
                for(int i=1;i<=numberOfPage;i++)
                    generatePagenumber(i);
                hasnextpage = false;
            }           
            generateNextLink(selectedPage);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //    public String toprevPage() {
    //        if (Global.getPageFlowScope("currentPage") != null)
    //            currentPage = (Integer)Global.getPageFlowScope("currentPage");
    //        if (currentPage > 1) {
    //            selectedPage = currentPage - 1;
    //            getListWithPaging(selectedPage);
    //        }
    //        return null;
    //    }

//    public String nextPage() {
//        if (Global.getPageFlowScope("currentPage") != null)
//            currentPage = (Integer)Global.getPageFlowScope("currentPage");
//        selectedPage = currentPage + 1;
//        getListWithPaging(selectedPage);
//
//        return null;
//    }

    public void setSohieuvanban(String sohieuvanban) {
        this.sohieuvanban = sohieuvanban;
    }

    public String getSohieuvanban() {
        return sohieuvanban;
    }

    public void setTrichyeu(String trichyeu) {
        this.trichyeu = trichyeu;
    }

    public String getTrichyeu() {
        return trichyeu;
    }

    public void setTungay(Date tungay) {
        this.tungay = tungay;
    }

    public Date getTungay() {
        return tungay;
    }

    public void setDenngay(Date denngay) {
        this.denngay = denngay;
    }

    public Date getDenngay() {
        return denngay;
    }

    public void setNoidung(String noidung) {
        this.noidung = noidung;
    }

    public String getNoidung() {
        return noidung;
    }

    public void setLstLinhvuc(List<DataObject> lstLinhvuc) {
        this.lstLinhvuc = lstLinhvuc;
    }

    public List<DataObject> getLstLinhvuc() {
        return lstLinhvuc;
    }

    public void setLstLoaivanban(List<DataObject> lstLoaivanban) {
        this.lstLoaivanban = lstLoaivanban;
    }

    public List<DataObject> getLstLoaivanban() {
        return lstLoaivanban;
    }

    public void setLstCoquanbanhanh(List<DataObject> lstCoquanbanhanh) {
        this.lstCoquanbanhanh = lstCoquanbanhanh;
    }

    public List<DataObject> getLstCoquanbanhanh() {
        return lstCoquanbanhanh;
    }

    public void setLstVBCK(List<DataObject> lstVBCK) {
        this.lstVBCK = lstVBCK;
    }

    public List<DataObject> getLstVBCK() {
        return lstVBCK;
    }

    public void setLstLinhvucItems(List<SelectItem> lstLinhvucItems) {
        this.lstLinhvucItems = lstLinhvucItems;
    }

    public List<SelectItem> getLstLinhvucItems() {
        if (!functionRequest.contains("getLstLinhvucItems")) {
            getdslinhvuc();
            functionRequest += "getLstLinhvucItems;";
        }
        return lstLinhvucItems;
    }

    public void setLstLoaivanbanItems(List<SelectItem> lstLoaivanbanItems) {
        this.lstLoaivanbanItems = lstLoaivanbanItems;
    }

    public List<SelectItem> getLstLoaivanbanItems() {
        if (!functionRequest.contains("getLstLoaivanbanItems")) {
            getdsloaibanban();
            functionRequest += "getLstLoaivanbanItems;";
        }
        return lstLoaivanbanItems;
    }

    public void setLstCoquanbanhanhItems(List<SelectItem> lstCoquanbanhanhItems) {
        this.lstCoquanbanhanhItems = lstCoquanbanhanhItems;
    }

    public List<SelectItem> getLstCoquanbanhanhItems() {
        if (!functionRequest.contains("getLstCoquanbanhanhItems")) {
            getdscoquanbanhanh();
            functionRequest += "getLstCoquanbanhanhItems;";
        }
        return lstCoquanbanhanhItems;
    }

    public void setLinhvucValue(String linhvucValue) {
        this.linhvucValue = linhvucValue;
    }

    public String getLinhvucValue() {
        return linhvucValue;
    }

    public void setLoaivanbanValue(String loaivanbanValue) {
        this.loaivanbanValue = loaivanbanValue;
    }

    public String getLoaivanbanValue() {
        return loaivanbanValue;
    }

    public void setCoquanbanhanhValue(String coquanbanhanhValue) {
        this.coquanbanhanhValue = coquanbanhanhValue;
    }

    public String getCoquanbanhanhValue() {
        return coquanbanhanhValue;
    }

    public void setFunctionRequest(String functionRequest) {
        this.functionRequest = functionRequest;
    }

    public String getFunctionRequest() {
        return functionRequest;
    }

    public void setRowkeyselect(RowKeySet rowkeyselect) {
        this.rowkeyselect = rowkeyselect;
    }

    public RowKeySet getRowkeyselect() {
        return rowkeyselect;
    }

    public void setVbckTable(RichTable vbckTable) {
        this.vbckTable = vbckTable;
    }

    public RichTable getVbckTable() {
        return vbckTable;
    }

    public String timKiemButtonClick() {
        try {
//            if (pagesnumber.size() > 0)
//                pagesnumber.clear();
            getListWithPaging(1);
            AdfFacesContext.getCurrentInstance().addPartialTarget(vbckTable);
            AdfFacesContext.getCurrentInstance().addPartialTarget(pagingPanelGroup);
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return null;
    }

    public void setChitieturl(String chitieturl) {
        this.chitieturl = chitieturl;
    }

    public String getChitieturl() {
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

            chitieturl = request.getParameter("url");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return chitieturl;
    }

    public void setNgaybanhanh(Date ngaybanhanh) {
        this.ngaybanhanh = ngaybanhanh;
    }

    public Date getNgaybanhanh() {
        Object obj = JSFUtils.resolveExpression("#{row.xmNgaybanhanh}");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        try {
            ngaybanhanh = format.parse(obj.toString());
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(timezone);
            cal.setTime(ngaybanhanh);
            cal.add(cal.HOUR, 7);
            ngaybanhanh = cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ngaybanhanh;
    }

    public void setTieude(String tieude) {
        this.tieude = tieude;
    }

    public String getTieude() {
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            tieude = request.getParameter("tieude");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return tieude;
    }


    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getCurrentPage() {
        if (Global.getPageFlowScope("currentPage") != null)
            currentPage = (Integer)Global.getPageFlowScope("currentPage");

        currentPage = selectedPage;
        if (currentPage == 1)
            firstpage = true;
        else
            firstpage = false;
        return currentPage;
    }

    public void setSelectedPage(int selectedPage) {
        this.selectedPage = selectedPage;
    }

    public int getSelectedPage() {

        return selectedPage;
    }


    public void setNumberOfResult(int numberOfResult) {
        this.numberOfResult = numberOfResult;
    }


    public void prevPage(ActionEvent actionEvent) {
        if (Global.getPageFlowScope("currentPage") != null)
            currentPage = (Integer)Global.getPageFlowScope("currentPage");
        if (currentPage > 1) {
            selectedPage = currentPage - 1;
            getListWithPaging(selectedPage);
        }
    }

    public void setPagesnumber(List<String> pagesnumber) {
        this.pagesnumber = pagesnumber;
    }

    public List<String> getPagesnumber() {
        if (Global.getPageFlowScope("pagesnumber") != null)
            pagesnumber = (List<String>)Global.getPageFlowScope("pagesnumber");
        if (pagesnumber.size() == 1)
            pagesnumber.clear();
        return pagesnumber;
    }

    public void setHasnextpage(boolean hasnextpage) {
        this.hasnextpage = hasnextpage;
    }

    public boolean isHasnextpage() {
        if (Global.getPageFlowScope("hasnextpage") != null)
            hasnextpage = (Boolean)Global.getPageFlowScope("hasnextpage");
        return hasnextpage;
    }


    public void setInputcurrentpage(RichInputText inputcurrentpage) {

        this.inputcurrentpage = inputcurrentpage;
    }

    public RichInputText getInputcurrentpage() {
        return inputcurrentpage;
    }

    public void setInputhasnext(RichInputText inputhasnext) {
        this.inputhasnext = inputhasnext;
    }

    public RichInputText getInputhasnext() {
        return inputhasnext;
    }


    public void testaction(ActionEvent actionEvent) {
        getListWithPaging(selectedPage);
    }

    public void setTempcurrenpage(String tempcurrenpage) {
        this.tempcurrenpage = tempcurrenpage;
    }

    public String getTempcurrenpage() {
        return tempcurrenpage;
    }

    public void setTempselectpage(String tempselectpage) {
        this.tempselectpage = tempselectpage;
    }

    public String getTempselectpage() {
        return tempselectpage;
    }

    public String pageSeleted() {
        //        if (tempselectpage != null && tempselectpage.length() > 0)
        //            selectedPage = Integer.parseInt(tempselectpage);

        getListWithPaging(selectedPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(vbckTable);
        AdfFacesContext.getCurrentInstance().addPartialTarget(pagingPanelGroup);

        return null;
    }


    public void setFirstpage(boolean firstpage) {
        this.firstpage = firstpage;
    }

    public boolean isFirstpage() {
        if (Global.getPageFlowScope("currentPage") != null)
            currentPage = (Integer)Global.getPageFlowScope("currentPage");
        if (currentPage == 1)
            firstpage = true;
        else
            firstpage = false;
        return firstpage;
    }


    public void setNgonnguid(String ngonnguid) {
        this.ngonnguid = ngonnguid;
    }

    public String getNgonnguid() {
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            ngonnguid = request.getParameter("ngonnguid").toString();

        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
        return ngonnguid;
    }

    public void setPagingPanelGroup(RichPanelBorderLayout pagingPanelGroup) {
        this.pagingPanelGroup = pagingPanelGroup;
    }

    public RichPanelBorderLayout getPagingPanelGroup() {
        return pagingPanelGroup;
    }

    public void pageSeleted(ActionEvent actionEvent) {
        selectedPage =
                Integer.parseInt(actionEvent.getComponent().getAttributes().get("selectedpage").toString());        
        getListWithPaging(selectedPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(vbckTable);
        AdfFacesContext.getCurrentInstance().addPartialTarget(pagingPanelGroup);
    }

    public void toprevPage(ActionEvent actionEvent) {
        selectedPage =
                Integer.parseInt(actionEvent.getComponent().getAttributes().get("prevpage").toString());        
        getListWithPaging(selectedPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(vbckTable);
        AdfFacesContext.getCurrentInstance().addPartialTarget(pagingPanelGroup);

    }

    public void nextPage(ActionEvent actionEvent) {
        selectedPage =
                Integer.parseInt(actionEvent.getComponent().getAttributes().get("nexpage").toString());
        getListWithPaging(selectedPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(vbckTable);
        AdfFacesContext.getCurrentInstance().addPartialTarget(pagingPanelGroup);
    }

    public void setInitpram(String initpram) {
        this.initpram = initpram;
    }

    public String getInitpram() {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if(request.getPortletSession().getAttribute("reset")!=null) {
           String flag = request.getPortletSession().getAttribute("reset").toString();
           if(flag.equals("1")) {
               firstpage = true;
               currentPage = 1;
               selectedPage = 1;
               numberOfResult = 10;
               hasnextpage = false;  
               tungay = null;
               denngay =null;
               sohieuvanban ="";
               linhvucValue ="";
               loaivanbanValue="";
               coquanbanhanhValue="";
               noidung="";
               getListWithPaging(1);  
               txtNoidung.setValue("");
               txtSohieuvanban.setValue("");
               txtTrichyeu.setValue("");
           }
        }
        return initpram;
    }

    public void setTimezone(TimeZone timezone) {
        this.timezone = timezone;
    }

    public TimeZone getTimezone() {
        return timezone;
    }

    public void setPortalLocale(Locale portalLocale) {
        this.portalLocale = portalLocale;
    }

    public void setTxtSohieuvanban(RichInputText txtSohieuvanban) {
        this.txtSohieuvanban = txtSohieuvanban;
    }

    public RichInputText getTxtSohieuvanban() {
        return txtSohieuvanban;
    }

    public void setTxtTrichyeu(RichInputText txtTrichyeu) {
        this.txtTrichyeu = txtTrichyeu;
    }

    public RichInputText getTxtTrichyeu() {
        return txtTrichyeu;
    }

    public void setTxtNoidung(RichInputText txtNoidung) {
        this.txtNoidung = txtNoidung;
    }

    public RichInputText getTxtNoidung() {
        return txtNoidung;
    }

    public Locale getPortalLocale() {
        return portalLocale;
    }
}
